<?php
namespace tencentcloud;

use helper\Http;
use helper\Str;

class Im
{
  private $appId;
  private $apiUrl = "https://console.tim.qq.com/v4/";
  private $identifier = 'administrator';
  public function __construct()
  {
    $this->appId = config('cloud.tencent.trtc.appid');
  }

  /**
   * 获取签名
   * @return mixed
   */
  public function getUserSig(){
    return D('live.Tic')->genSig($this->identifier);
  }

  /**
   * 获取请求地址
   * @param $apiName
   * @return string
   */
  private function requestUrl($apiName){
    $url = $this->apiUrl.$apiName;
    $params = [
        'sdkappid'=>$this->appId,
        'identifier'=>$this->identifier,
        'usersig'=>$this->getUserSig(),
        'random'=>Str::random(),
        'contenttype'=>'json'
    ];
    $url .= '?'.http_build_query($params);
    return $url;
  }

  /**
   * 获取群组列表
   * @return mixed
   */
  public function getGroupList(){
    $url = $this->requestUrl('group_open_http_svc/get_appid_group_list');
    $result = Http::post($url,'{}');
    return json_decode($result,true);
  }

  /**
   * 获取群组详细资料
   * @param $groupId
   * @return mixed
   */
  public function modifyGroupBaseInfo($groupId){
    $url = $this->requestUrl('group_open_http_svc/modify_group_base_info');
    $params = [
        "GroupId"=>$groupId,
        "MaxMemberNum"=>2000
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 获取群组详细资料
   * @param string $groupIds //数组或者字符串
   * @return mixed
   */
  public function getGroupInfo($groupIds){
    $url = $this->requestUrl('group_open_http_svc/get_group_info');
    $params = [
        "GroupIdList"=> is_array($groupIds) ? $groupIds : explode(',',$groupIds)
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 解散群组
   * @param $groupId
   * @return mixed
   */
  public function destroyGroup($groupId){
    $url = $this->requestUrl('group_open_http_svc/destroy_group');
    $params = [
        "GroupId"=> $groupId
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 获取用户在线状态
   * @param $toAccount //数组或者字符串
   * @return mixed
   */
  public function querystate($toAccount){
    $url = $this->requestUrl('openim/querystate');
    $params = [
        "To_Account"=>is_array($toAccount) ? $toAccount : explode(',',$toAccount)
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 获取群组会员详细资料
   * @param $groupId
   * @param $page
   * @param int $limit
   * @return mixed
   */
  public function getGroupMemberInfo($groupId,$page=1,$limit=100){
    $url = $this->requestUrl('group_open_http_svc/get_group_member_info');
    $offset = ($page-1)*$limit;
    $params = [
        'GroupId'=>(string)$groupId,
        'Limit'=>$limit,
        'Offset'=>$offset,
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 设置资料
   * @param $FromAccount
   * @param $info
   * @return mixed
   */
  public function setPortrait($FromAccount,$info){
    $url = $this->requestUrl('/profile/portrait_set');
    $profileItem = [];
    if(isset($info['nick'])) $profileItem[] = ['Tag'=>'Tag_Profile_IM_Nick','Value'=>$info['nick']];
    if(isset($info['avatar'])) $profileItem[] = ['Tag'=>'Tag_Profile_IM_Image','Value'=>$info['avatar']];
    if(isset($info['level'])) $profileItem[] = ['Tag'=>'Tag_Profile_IM_Level','Value'=>(int)$info['level']];
    if(isset($info['role'])) $profileItem[] = ['Tag'=>'Tag_Profile_IM_Role','Value'=>(int)$info['role']];
    if(isset($info['signature'])) $profileItem[] = ['Tag'=>'Tag_Profile_IM_SelfSignature','Value'=>$info['signature']];
    if(!empty($profileItem)){
      $params = [
          'From_Account'=>$FromAccount,
          "ProfileItem"=>$profileItem
      ];
      $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
      return json_decode($result,true);
    }else{
      return false;
    }
  }

  /**
   * 获取资料
   * @param $toAccount
   * @return mixed
   */
    public function getPortrait($toAccount){
        $url = $this->requestUrl('profile/portrait_get');
        $params = [
            'To_Account'=>explode(',',$toAccount),
            'TagList'=>[
                "Tag_Profile_IM_Nick",
                "Tag_Profile_IM_Image",
                "Tag_Profile_IM_Role",
                "Tag_Profile_IM_SelfSignature"
            ]
        ];
        $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
        return json_decode($result,true);
    }

  /**
   * 批量禁言和取消禁言
   * @param $groupId
   * @param $MembersAccount //禁言账户 数组或字符串
   * @param int $ShutUpTime //禁言时间秒
   * @return mixed
   */
  public function forbidSendMsg($groupId,$MembersAccount,$ShutUpTime){
    $url = $this->requestUrl('/group_open_http_svc/forbid_send_msg');
    $params = [
        'GroupId'=>(string)$groupId,
        'Members_Account'=> is_array($MembersAccount) ? $MembersAccount : explode(',',$MembersAccount),
        'ShutUpTime'=>$ShutUpTime
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }


  /**
   * 获取群组被禁言用户列表
   * @param $groupId
   * @return mixed
   */
  public function getGroupShuttedUin($groupId){
    $url = $this->requestUrl('/group_open_http_svc/get_group_shutted_uin');
    $params = [
        'GroupId'=>(string)$groupId,
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }


  /**
   * 在群组中发送系统通知
   * @param $groupId
   * @param $content
   * @return mixed
   */
  public function sendGroupSystemNotification($groupId,$content){
    $url = $this->requestUrl('/group_open_http_svc/send_group_system_notification');
    $params = [
        'GroupId'=>(string)$groupId,
        'Content'=>$content,
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 在群组中发送普通通知
   * @param $groupId
   * @param $MsgBody
   * @param string $msgPriority 消息优先级
   * @return mixed
   */
  public function sendGroupMsg($groupId,$MsgBody,$msgPriority='High'){
    $url = $this->requestUrl('/group_open_http_svc/send_group_msg');
    $params = [
        'GroupId'=>(string)$groupId,
        'Random'=>(int)Str::random(8,1),
        "MsgPriority"=>$msgPriority, // 消息优先级
        'MsgBody'=>$MsgBody
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 拉取群组最新消息
   * @param $groupId
   * @param int $number
   * @return mixed
   */
  public function getGroupMsg($groupId,$number=100){
    $url = $this->requestUrl('/group_open_http_svc/group_msg_get_simple');
    $params = [
        'GroupId'=>(string)$groupId,
        'ReqMsgNumber'=>$number,
    ];
    $result = Http::post($url,json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }
}